package com.sqstudio.components
{
	import flash.display.DisplayObject;
	import flash.display.DisplayObjectContainer;
	import flash.display.SimpleButton;
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	import flash.filters.ColorMatrixFilter;
	
	import gs.TweenLite;
	import gs.plugins.FastTransformPlugin;
	
	/**
	 *图像按钮类
	 * 
	 * <br/> 单帧素材，支持滑过放大和不可用灰度滤镜
	 *  
	 * @author Nestor
	 * 
	 */
	public class ImageButton extends Component
	{
		protected var _back:Sprite;
		protected var _face:Sprite;
		
		protected var _upState:DisplayObject;
		protected var _downState:DisplayObject;
		protected var _overState:DisplayObject;
		protected var _enableState:DisplayObject;
		private var _enableFilter:Boolean;
		private var _moveScale:Boolean;
		
		
		/**
		 * Constructor
		 * @param parent The parent DisplayObjectContainer on which to add this PushButton.
		 * @param xpos The x position to place this component.
		 * @param ypos The y position to place this component.
		 * @param label The string to use for the initial label of this component.
		 * @param defaultHandler The event handling function to handle the default event for this component (click in this case).
		 */
		public function ImageButton(parent:DisplayObjectContainer = null, xpos:Number = 0, ypos:Number =  0, defaultHandler:Function = null,enableFilter:Boolean=false,moveScale:Boolean=false)
		{
			super(parent, xpos, ypos);
			this.moveScale = moveScale;
			this.enableFilter = enableFilter;
			if(defaultHandler != null)
			{
				addEventListener(MouseEvent.CLICK, defaultHandler);
			}
		}
		
		/**
		 * Initializes the component.
		 */
		override protected function init():void
		{
			super.init();
			buttonMode = true;
			useHandCursor = true;
			setSize(100, 20);
		}
		
		/**
		 * Creates and adds the child display objects of this component.
		 */
		override protected function addChildren():void
		{
			_back = new Sprite();
			addChild(_back);
			_face = new Sprite();
			addChild(_face);
			
			addEventListener(MouseEvent.MOUSE_DOWN, onMouseGoDown);
			addEventListener(MouseEvent.ROLL_OVER, onMouseOver);
		}
		
		
		
		///////////////////////////////////
		// public methods
		///////////////////////////////////
		
		/**
		 * Draws the visual ui of the component.
		 */
		override public function draw():void
		{
			super.draw();
			_back.graphics.clear();
			_back.graphics.beginFill(0xCCCCCC);
			_back.graphics.drawRect(0, 0, _width, _height);
			_back.graphics.endFill();
			_face.graphics.clear();
			_face.graphics.beginFill(0xEEEEEE,0);
			_face.graphics.drawRect(0, 0, _width - 2, _height - 2);
			_face.graphics.endFill();
			
			_back.x = -_back.width/2;
			_back.y = -_back.height/2;
			_face.x = -_face.width/2;
			_face.y = -_face.height/2;
		}
		/**
		 *设置按钮皮肤 
		 */		
		public function setBtnSkin(skin:DisplayObject):void{
			_back.addChild(skin);
			setSize(skin.width,skin.height);
		}
		
		
		///////////////////////////////////
		// event handlers
		///////////////////////////////////
		
		/**
		 * Internal mouseOver handler.
		 * @param event The MouseEvent passed by the system.
		 */
		protected function onMouseOver(event:MouseEvent):void
		{
			addEventListener(MouseEvent.ROLL_OUT, onMouseOut);
			
			if(moveScale){
				TweenLite.to(this,0.5,{scaleX:1.1,scaleY:1.1});
			}
		}
		
		/**
		 * Internal mouseOut handler.
		 * @param event The MouseEvent passed by the system.
		 */
		protected function onMouseOut(event:MouseEvent):void
		{
			if(moveScale){
				TweenLite.to(this,0.5,{scaleX:1,scaleY:1});
			}
		}
		
		/**
		 * Internal mouseOut handler.
		 * @param event The MouseEvent passed by the system.
		 */
		protected function onMouseGoDown(event:MouseEvent):void
		{
			addEventListener(MouseEvent.MOUSE_UP, onMouseGoUp);
		}
		
		/**
		 * Internal mouseUp handler.
		 * @param event The MouseEvent passed by the system.
		 */
		protected function onMouseGoUp(event:MouseEvent):void
		{
		}
		
		
		
		
		///////////////////////////////////
		// getter/setters
		///////////////////////////////////
		/**
		 * Sets/gets whether this component is enabled or not.
		 */
		override public function set enabled(value:Boolean):void
		{
			mouseEnabled = mouseChildren = value;
			tabEnabled = value;
			if(enableFilter  && !value){
				this.filters = GRAY_FILTER;
			}
			if(value){
				this.filters = null;
			}
		}

		public function get enableFilter():Boolean
		{
			return _enableFilter;
		}

		public function set enableFilter(value:Boolean):void
		{
			_enableFilter = value;
		}

		public function get moveScale():Boolean
		{
			return _moveScale;
		}

		public function set moveScale(value:Boolean):void
		{
			_moveScale = value;
		}

		
		//【泛用】 灰度滤镜
		public static const GRAY_FILTER:Array = 
			[new ColorMatrixFilter([ 
				0.3, 0.3, 0.3, 0, 0,
				0.3, 0.3, 0.3, 0, 0,
				0.3, 0.3, 0.3, 0, 0, 
				0, 0, 0, 1, 0		
			])];
	}
}